﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for addBarang.xaml
    /// </summary>
    public partial class addBarang : Window
    {
        private string ID = "";
        private bool isUbah = false;
        private DataSet ubah = new DataSet();

        public addBarang()
        {
            InitializeComponent();
        }

        public addBarang(string ID)
        {
            this.ID = ID;
            isUbah = true;
            InitializeComponent();
            label1.Content = "Edit Item";
            addButton.Content = "Edit";
            cancelButton.Content = "Cancel";
            Title = "TAMS UP :: Form :: Edit Item";
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                homeWindow.Con.Open();

                if (!isUbah)
                {
                    string _Insert = @"insert into daftar_barang (ID_Barang,Nama_Barang,Total,Keterangan)
                              Values('I" + "','" + textBox1.Text + "','" + textBox3.Text + "','" +
                                             textBox2.Text + "');";

                    SqlCommand _cmd = new SqlCommand(_Insert, homeWindow.Con);

                    // Execute the command
                    _cmd.ExecuteNonQuery();

                    MessageBox.Show("One Record Inserted");
                }
                else
                {
                    string _Insert = @"update daftar_barang
                                        set Nama_Barang = '" + textBox1.Text + "', Total = '" + textBox3.Text + "',  Keterangan = '" + textBox2.Text +
                                                        "' where ID_Barang = '" + ID + "';";

                    SqlCommand _cmd = new SqlCommand(_Insert, homeWindow.Con);
                    _cmd.ExecuteNonQuery();
                    MessageBox.Show("One Record Edited");
                }
                homeWindow.Con.Close();
                homeWindow.top.refresh();
                homeWindow.top.Focus();
                this.Close();
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (isUbah)
            {
                homeWindow.top.refresh();
                homeWindow.top.Focus();
                this.Close();
            }
            else
            {
                
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (isUbah)
            {
                try
                {
                    homeWindow.Con.Open();

                    SqlDataAdapter da = new SqlDataAdapter("select * from daftar_barang where ID_Barang = '" + ID + "';", homeWindow.Con);
                    ubah = new DataSet();
                    da.Fill(ubah, "Daftar Barang");
                    textBox1.Text = ubah.Tables["Daftar Barang"].Rows[0].ItemArray.ElementAt(1).ToString();
                    textBox2.Text = ubah.Tables["Daftar Barang"].Rows[0].ItemArray.ElementAt(3).ToString();
                    textBox3.Text = ubah.Tables["Daftar Barang"].Rows[0].ItemArray.ElementAt(2).ToString();
                    homeWindow.Con.Close();
                }
                catch (SqlException sqle)
                {
                    MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
                }
            }
        }

        private void formWindow_Closed(object sender, EventArgs e)
        {
            homeWindow.top.refresh();
            homeWindow.top.Focus();
        }

        private string addNol(int x)
        {
            string temp = "";
            string angka = x.ToString();

            if (angka.Count() == 4)
            {
                temp = "0";
            }
            else if (angka.Count() == 3)
            {
                temp = "00";
            }
            else if (angka.Count() == 2)
            {
                temp = "000";
            }
            else if (angka.Count() == 1)
            {
                temp = "0000";
            }
            Console.WriteLine(temp);
            return temp;
        }

        private Boolean errorChecking()
        {
            Boolean valid = true;
            Brush invalidColor = new SolidColorBrush(Color.FromRgb(255, 225, 0));
            Brush ValidColor = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            int temp;
            if ((textBox1.Text == "") || (textBox1.Text.Length > 25))
            {
                textBox1.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                textBox1.Background = ValidColor;
            }
            if (textBox3.Text == "")
            {
                textBox3.Background = invalidColor;
                if (valid) valid = false;
            }
            else if (!Int32.TryParse(textBox3.Text.ToString(), out temp))
            {
                textBox3.Background = invalidColor;
                if (valid) valid = false;
            }
            else
            {
                textBox3.Background = ValidColor;
            }
            if (!valid)
            {
                MessageBox.Show("Ada kesalahan pengisian informasi tambah reklame.\nTidak boleh ada yang kosong dan nama barang tidak boleh lebih dari 25 karakter.", "Invalid Information");
            }
            return valid;
        }
    }

    
}
